package com.kc.sjwh.service;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kc.sjwh.dao.CzllzDao;
import com.kc.sjwh.dao.CzllzcyDao;
import com.kc.sjwh.dao.OrganDao;
import com.kc.sjwh.dao.PdtDao;
import com.kc.sjwh.dao.PdtthzDao;
import com.kc.sjwh.dao.RyDao;
import com.kc.sjwh.dao.SysDao;
import com.kc.sjwh.dao.ZdpzxxDao;
import com.kc.sjwh.entity.po.Czllzcy;
import com.kc.sjwh.entity.po.Organ;
import com.kc.sjwh.entity.po.Pdt;
import com.kc.sjwh.entity.po.Pdtthz;
import com.kc.sjwh.entity.po.Ry;
import com.kc.sjwh.entity.po.Zdpzxx;
import com.kc.sjwh.entity.vo.dict.DictItem;
import com.kc.sjwh.other.Page;
import com.kc.sjwh.other.QueryParam;
import com.kc.sjwh.other.Tool;

/**
 * 处置力量组成员 service
 * @author xianding
 * @date 2017年11月28日
 */
@Service
public class CzllzcyService {
	static Logger logger = Logger.getLogger(CzllzcyService.class);
	@Autowired
	CzllzcyDao czllzcyDao;
	@Autowired
	CzllzDao czllzDao;
	@Autowired
	OrganDao organDao;
	@Autowired
	SysDao sysDao;
	@Autowired
	RyDao ryDao;
	@Autowired
	PdtDao pdtDao;
	@Autowired
	PdtthzDao pdtthzDao;
	@Autowired
	ZdpzxxDao zdpzxxDao;

	/**
	 * 分页查询
	 * @param pageSize
	 * @param pageNum
	 * @param param
	 * @return
	 */
	public Page<Czllzcy> getPage(int pageSize, int pageNum, Map<String, String> param) {
		QueryParam q = new QueryParam(pageSize, pageNum).setOrder("t1.gxsj desc");
		
		//添加动态的查询条件
		String v = null;
		v = param.get("startTime");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition(" t1.gxsj >= to_date('"+v+"','yyyy-mm-dd hh24:mi')");
		}
		v = param.get("endTime");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition(" t1.gxsj <= to_date('"+v+"','yyyy-mm-dd hh24:mi')");
		}
		v = param.get("keyword");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition("t1.cydxmc like ?".replace("?", "'%"+v+"%'"));
		}
		v = param.get("dwdm");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition("t1.cjdwdm='"+ v+"'");
		}		
		v = param.get("cylx");
		if(StringUtils.isNotEmpty(v)) {
			q.addSqlCondition("t1.cylxdm='"+ v+"'");
		}		
		return czllzcyDao.getPage("get", q);
	}

	/**
	 * 新增
	 * @param one
	 */
	public void add(Czllzcy one) {
		one.setCjsj(new Date());
		one.setCybh(Tool.getUuid());
		int num= czllzcyDao.count(new QueryParam()
				.put("t1.zbh", one.getZbh())
				.put("t1.cydxbh", one.getCydxbh())			
				);
		if (num==0 ) {		
			czllzcyDao.add(one);
		}
	}

	/**
	 * 修改
	 * @param one
	 */
	public void update(Czllzcy one) {
		one.setGxsj(new Date());
		czllzcyDao.update(one, "cybh");
	}

	/**
	 * 删除
	 * @param id
	 */
	@Transactional
	public void delete(String id) {
		//级联删除 none
		Czllzcy one =new Czllzcy();
		one.setCybh(id);
		one.setSybz("N");
		one.setGxsj(new Date());
		czllzcyDao.update(one, "cybh");
		czllzDao.updateByYbh(id);
		//删除
//		int n = czllzcyDao.delete(id);
//		if(n == 0) {
//			throw new IllegalArgumentException("要删除的记录不存在");
//		}
	}

	/**
	 * 打开表单
	 * @param id
	 * @return
	 */
	public Czllzcy form(String id) {
		if (id!=null) {
			Czllzcy one=czllzcyDao.getOne(new QueryParam().put("t1.cybh", id));
			List<DictItem> list=sysDao.getDict("行政区划");
			for (DictItem dictItem : list) {
				if (dictItem.getId().equals(one.getXzqh())) {
					one.setXzqhmc(dictItem.getName());
					break;
				}
			}
			return one;
		}		
		return  null;
	}

	/**
	 * 根据行设置处置力量组
	 */
//	public void setCzllcyByRow(HSSFRow row, Czllzcy one) {
//		
//	}

	/**
	 * 获取成员目录
	 */
	public List<DictItem> getAllAsDict() {
		List<DictItem> list = new ArrayList<DictItem>();
		DictItem type1 = new DictItem();
		type1.setId("01");
		type1.setName("表单位");
		type1.setNocheck(true);
		list.add(type1);
		DictItem type2= new DictItem();
		type2.setId("02");
		type2.setName("人员信息");
		type2.setNocheck(true);
		list.add(type2);
		DictItem type3= new DictItem();
		type3.setId("03");
		type3.setName("PDT");
		type3.setNocheck(true);
		list.add(type3);
		DictItem type4= new DictItem();
		type4.setId("04");
		type4.setName("终端");
		type4.setNocheck(true);
		list.add(type4);
		DictItem type5= new DictItem();
		type5.setId("05");
		type5.setName("PDT组");
		type5.setNocheck(true);
		list.add(type5);
		Map<String, String> dwMap=new HashMap<String, String>();
		for (Organ bean : organDao.getList(new QueryParam())) {
			DictItem di = new DictItem();
			di.setId(bean.getDwdm());
			di.setName("01-"+bean.getDwjc());
			if (bean.getGxdwdm()==null) {
				di.setPid("01");
			}else {
				di.setPid(bean.getGxdwdm());
			}
			dwMap.put(bean.getDwdm(),bean.getDwjc());
			list.add(di);
		}
		List<Ry> ryList=ryDao.getList(new QueryParam().setOrder("ssdwdm"));
		Set<String> ry_dw=new HashSet<>();
		for (Ry bean : ryList) {			
			if (!ry_dw.contains(bean.getSsdwdm())) {
				ry_dw.add(bean.getSsdwdm());
				DictItem di = new DictItem();
				di.setId("02-"+bean.getSsdwdm());
				di.setName(dwMap.get(bean.getSsdwdm()));
				di.setPid("02");
				di.setNocheck(true);
				list.add(di);
			}
		}
		for (Ry bean : ryList) {
			DictItem di = new DictItem();
			di.setId(bean.getRybh());
			di.setName("02-"+bean.getRyxm());
			di.setPid("02-"+bean.getSsdwdm());
			list.add(di);
		}
		List<Pdt> pdtList=pdtDao.getList(new QueryParam().setOrder("zzjgdm"));
		Set<String> pdt_dw=new HashSet<>();
		for (Pdt bean : pdtList) {
			if (!pdt_dw.contains(bean.getZzjgdm())) {
				pdt_dw.add(bean.getZzjgdm());
				DictItem di = new DictItem();
				di.setId("03-" + bean.getZzjgdm());
				di.setName(dwMap.get(bean.getZzjgdm()));
				di.setPid("03");
				di.setNocheck(true);
				list.add(di);
			}
		}
		for (Pdt bean : pdtList) {
			DictItem di = new DictItem();
			di.setId(bean.getSbbh());
			di.setName("03-"+bean.getSbbh());
			di.setPid("03-"+bean.getZzjgdm());
			list.add(di);
		}
		List<Zdpzxx> zdpzxxList=zdpzxxDao.getList(new QueryParam());
		for (Zdpzxx bean : zdpzxxList) {
			DictItem di = new DictItem();
			di.setId(bean.getZdjqip());
			di.setName("04-"+bean.getZdmc());
			di.setPid("04");
			list.add(di);
		}
		for (Pdtthz bean : pdtthzDao.getList(new QueryParam())) {
			DictItem di = new DictItem();
			di.setId(bean.getThzbh());
			di.setName("05-"+bean.getThzmc());
			di.setPid("05");
			list.add(di);
		}
		return list;
	}

	public String getZmcByBh(String id) {
		return czllzDao.getOne(new QueryParam().put("zbh", id)).getZmc();
	}
		
}
